package com.kaimy.controller 
{
	import com.kaimy.model.ImageDescriptor;
	import com.kaimy.model.vo.ModelVO;
	import com.kaimy.model.OpenModel;
	import flash.events.Event;
	import flash.display.Sprite;

	/**
	 * @author jbonfante
	date: Aug 2, 2009
	for: Roundarch Inc.
 
	Document Information:
	Project: KaimysAwesomeFlashProject
	Package: com.kaimy.controller.Main
	 */
	public class Main extends Sprite 
	{
		private var loadController:LoadController = LoadController.getInstance();
		private var model:OpenModel;
		
		public function Main()
		{
			loadController.add("data/TestFile.xml",{id:"config"}).addEventListener(Event.COMPLETE, LoadModel,false,0,true);
			loadController.start();
		}
		
		private function LoadModel(event:Event):void
		{
			removeEventListener(Event.COMPLETE, LoadModel);
			var modelData:XML = loadController.getXML("config");
			trace(modelData);
			model = new OpenModel(modelData);
			model.addEventListener(Event.COMPLETE, LoadImages,false,0,true);
		}
		
		private function LoadImages(event : Event) : void
		{
			removeEventListener(Event.COMPLETE, LoadImages);
			var imgAR:Array = model.imageArray;
			var length:Number = imgAR.length;
			var imgDescriptor:ImageDescriptor = model.imageDescriptor;
			var dir:String = imgDescriptor.directory;
			for(var i:int=0;i<length;i++)
			{
				var modelImage:ModelVO = ModelVO(imgAR[i]);
				var imgName:String = modelImage.image;
				loadController.add(dir+imgName,{id:modelImage.id}).addEventListener(Event.COMPLETE, ImageLoaded,false,0,true);
				if(i==length-1)loadController.start();
			}
		}
		
		private function ImageLoaded(event : Event) : void
		{
			trace("Loaded Image: " + event.target.id);
		}
	}
}
